<template>
  <div>
    <el-dialog
      :title="obj.isAdd ? '规格添加' : '规格编辑'"
      :visible.sync="obj.isTrue"
      @close="cancel()"
      width="50%"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="规格名称">
          <el-input v-model="form.specsname"></el-input>
        </el-form-item>
        <el-form-item
          label="规格属性"
          v-for="(item, index) in attrArr"
          :key="index"
        >
          <div class="attrbox">
            <el-input v-model="item.val"></el-input>
            <el-button type="primary" v-if="index == 0" @click="addAttr()"
              >新增规格属性</el-button
            >
            <el-button type="danger" v-else @click="preAttr(index)"
              >删 除</el-button
            >
          </div>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="submit()" v-if="obj.isAdd"
          >添 加</el-button
        >
        <el-button type="primary" @click="edit()" v-else>编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { successAlert, warningAlert } from "../../../util/alert.js";
import { specsAdd, specsInfo, specsEdit } from "../../../request/api";
export default {
  props: ["obj"],
  components: {},
  data() {
    return {
      // attrArr 用来标识要渲染多少栏规格属性
      attrArr: [{ val: "" }, { val: "" }],
      form: {
        specsname: "",
        attrs: "",
        status: 1,
      },
    };
  },
  computed: {
    ...mapGetters({}),
  },
  methods: {
    ...mapActions({
      reqSpecsList: "specs/reqChangeList",
      reqSpecsTotal: "specs/reqChangeTotal",
    }),
    // 封装清空
    empty() {
      this.form = {
        specsname: "",
        attrs: "",
        status: 1,
      };
      this.attrArr = [{ val: "" }, { val: "" }];
    },
    // 点击了取消
    cancel() {
      //关闭对话框
      this.obj.isTrue = false;
      //清空数据
      this.empty();
    },
    // 点击了新增规格属性
    addAttr() {
      this.attrArr.push({ val: "" });
      // console.log(this.attrArr);
    },
    // 点击了删除规格属性
    preAttr(index) {
      this.attrArr.splice(index, 1);
    },
    // 点击了添加
    submit() {
      this.form.attrs = JSON.stringify(this.attrArr.map((item) => item.val));
      specsAdd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.reqSpecsList();
          this.reqSpecsTotal();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    getOne(id) {
      specsInfo({ id: id }).then((res) => {
        this.attrArr = JSON.parse(res.data.list[0].attrs).map((item)=>({val: item}));
        this.form = res.data.list[0];
        this.form.id = id;
      });
    },
    //点击了确定编辑
    edit(id) {
      this.form.attrs = JSON.stringify(this.attrArr.map(item => item.val));
      specsEdit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.reqSpecsList();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
  },
  mounted() {},
};
</script>
<style scoped>
.attrbox {
  display: flex;
}
</style>